package com.example.keli.ui.center;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.keli.R;
import com.example.keli.api.RetrofitManager;
import com.example.keli.base.App;
import com.example.keli.base.BaseActivity;
import com.example.keli.base.app.C;
import com.example.keli.base.utils.DataUtil;
import com.example.keli.base.utils.GlideUtil;
import com.example.keli.base.utils.ToastUtil;
import com.example.keli.ui.my.model.UserInfo;
import com.example.keli.ui.userinfo.ApplyActivity;
import com.example.keli.ui.userinfo.PersonDataActivity;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import com.example.keli.api.http.api.BaseResponse;
import com.example.keli.api.http.api.subscriber.SubscriberListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class ActiveDetailsActivity extends BaseActivity {

    @BindView(R.id.iv_active_details)
    ImageView ivActiveDetails;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.tv_date)
    TextView tvDate;
    @BindView(R.id.tv_introduce)
    TextView tvIntroduce;
    @BindView(R.id.tv_sign)
    TextView tvSign;
    @BindView(R.id.tv_agent)
    TextView tvAgent;
    String aaid = "";
    private boolean isSign;//是否已报名
    protected final List<String> sStatus = Arrays.asList("待启动", "报名中", "进行中", "已结束");

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSignSucess(String event){
        if (C.EVENT.SIGN_SUCCESS.equals(event)){
           finish();
        }
    }

    public static void start(Context context, int id) {
        Intent intent = new Intent(context, ActiveDetailsActivity.class);
        intent.putExtra("id", id);
        context.startActivity(intent);
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_active_details;
    }

    @Override
    protected void bindData() {
        EventBus.getDefault().register(this);
//        setTitle("活动详情");
        abTitle.setText("活动详情");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        int id = getIntent().getIntExtra("id", 0);
        loadDatas(id);
    }

    private void loadDatas(int id) {
        Map<String, String> map = new HashMap<>();
        map.put("id", String.valueOf(id));
        RetrofitManager.ApiFunc(RetrofitManager.mApiService.getActivityInfo(map), new SubscriberListener<BaseResponse<ActivieList.DataBean>>() {
            @Override
            public void onSuccess(BaseResponse<ActivieList.DataBean> dataBeanBaseResponse) {
                ActivieList.DataBean dataBean = dataBeanBaseResponse.data;
                if (dataBean != null) {
//                    Glide.with(getApplication()).load(dataBean.getCover_picture()).into(ivActiveDetails);
                    GlideUtil.displayRound(getApplication(), ivActiveDetails, dataBean.getCover_picture(), 5);
                    tvName.setText(dataBean.getTitle());
                    tvStatus.setText(sStatus.get(dataBean.getStatus() - 1));
                    tvNum.setText("参与人数:  " + dataBean.getEnrollCount() + "人");
                    tvDate.setText("起止时间:  " + DataUtil.getData(dataBean.getStart_time()) + "-" + DataUtil.getData(dataBean.getEnd_time()));
                    tvIntroduce.setText(dataBean.getContent());
                    aaid = String.valueOf(dataBean.getId());
                    isSign = ("true".equals(dataBean.getIsSign()));
                }
            }

            @Override
            public void onFailer(String msg) {

            }

            @Override
            public void onTokenError() {

            }
        });
    }

    @OnClick({R.id.tv_sign, R.id.tv_agent})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_sign:
                if (TextUtils.isEmpty(aaid)) {
                    ToastUtil.show("请稍后再试");
                    return;
                }
                if (isSign){
                    ToastUtil.show("已经报过名了");
                    return;
                }
                getUserInfo();
                break;
            case R.id.tv_agent:
                RetrofitManager.ApiFunc(RetrofitManager.mApiService.applyProxy(), new SubscriberListener<BaseResponse<Object>>() {
                    @Override
                    public void onSuccess(BaseResponse<Object> baseResponse) {
                        toast(baseResponse.getMsg());
                    }

                    @Override
                    public void onFailer(String msg) {
                        toast(msg);
                    }

                    @Override
                    public void onTokenError() {

                    }
                });
                break;
        }
    }

    private void getUserInfo() {
        Map<String, String> map = new HashMap<>();
        map.put("uid", App.getUid());
        map.put("page", "1");
        RetrofitManager.ApiFunc(RetrofitManager.mApiService.getUser(map), new SubscriberListener<BaseResponse<UserInfo>>() {
            @Override
            public void onSuccess(BaseResponse<UserInfo> listBaseResponse) {
                UserInfo userInfo = listBaseResponse.data;
                if (userInfo != null) {
                    if (TextUtils.isEmpty(userInfo.getBirthday())) {
                        //空，必须先完善资料
                        PersonDataActivity.start(ActiveDetailsActivity.this);
                    } else {
                        ApplyActivity.start(getActivity(), aaid);
                    }
                }else{
                    PersonDataActivity.start(ActiveDetailsActivity.this);
                }
            }

            @Override
            public void onFailer(String msg) {
            }

            @Override
            public void onTokenError() {
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
